package com.doradance.app.home;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.account.ParamKey;
import com.doradance.account.UrlCenter;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.AllCommentsEntity;
import com.doradance.entity.FreeItemEntity;
import com.doradance.entity.UpdataDetailEntity;
import com.example.xlhratingbar_lib.XLHRatingBar;
import com.pachong.android.baseuicomponent.CompState;
import com.pachong.android.baseuicomponent.LoadState;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class AllCommentsFragment extends SwipeListFragment {
    private int last_id =0;
    private String course_id;
    private Boolean islearn;
    private int mPage = 0;
    public AllCommentsFragment() {
        // Required empty public constructor
    }

    @SuppressLint("ValidFragment")
    public AllCommentsFragment(String course_id,Boolean islearn) {
        // Required empty public constructor
        this.course_id = course_id;
        this.islearn = islearn;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        EventBus.getDefault().register(this);
        startLoading();
        setPullToRefreshEnable(false);
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new AllCommentsAdapter(getActivity());
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        getData().clear();
        last_id = 0;
        startLoading();
    }

    private ListDataRequestListener<AllCommentsEntity> mResponceListener = new ListDataRequestListener<>(this, AllCommentsEntity.class, false);

    @Override
    public void onStartLoading() {
        if (mResponceListener.getResponseDataParser().hasMore1()) {
            mPage++;
        }
        MyHttpRequest request = new MyHttpRequest(getContext());
        UrlParams urlParams = new UrlParams();
        Log.e("datadatadata",last_id+"");
        urlParams.put("course_id",course_id);
        urlParams.put("last_id", last_id+"");
        urlParams.put(ParamKey.SIZE, 10+"");
        request.get(UrlCenter.ALL_COMMENTS, urlParams, new DataRequestListener<List<AllCommentsEntity>>(AllCommentsEntity.class
        ) {

            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }
            @Override
            public void onSuccess(List<AllCommentsEntity> o) {
                super.onSuccess(o);
                setState(CompState.DATA);
                getAdapter().getData().addAll(o);
                getAdapter().notifyDataSetChanged();
            }

//            @Override
//            public void enableProgress(boolean enableProgress) {
//                super.enableProgress(enableProgress);
//                if(enableProgress){
//                    enableProgress(false);
//                }
//            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(UpdataDetailEntity refreshEvent) {
        Log.e("1111111111","sdfsdfsd");
        getData().clear();
        last_id = 0;
        getAdapter().getData().addAll(refreshEvent.o);
        getAdapter().notifyDataSetChanged();
    }

    class AllCommentsAdapter extends RecyclerViewBaseAdapter<AllCommentsEntity> {
        public AllCommentsAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new VH(setAdapterViewItem(parent, R.layout.add_comments));
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder viewHolder, int position) {
            VH holder = (VH) viewHolder;
            List<AllCommentsEntity> data = getData();
            if(mPage!=0){
                last_id = getData().get(data.size() - 1).getId();
            }
            AllCommentsEntity allCommentsEntity = getData().get(position);
            holder.mTvCommentsDetail.setText(allCommentsEntity.getContent());
            if(!TextUtils.isEmpty(data.get(position).getAvatar())){
                Glide.with(getContext()).load(data.get(position).getAvatar()).into(holder.mCivComments);
            }else{
                Glide.with(getContext()).load(R.mipmap.init_logo).into(holder.mCivComments);
            }
            if(!TextUtils.isEmpty(data.get(position).getNick_name())){
                holder.mTvCommentName.setText(data.get(position).getNick_name());
            }else{
                holder.mTvCommentName.setText("妞秀用户");
            }
            holder.mRatingbarIdComments.setCountSelected(data.get(position).getGrade());

        }

        public class VH extends RecyclerView.ViewHolder {
            @Bind(R.id.civ_comments)
            CircleImageView mCivComments;
            @Bind(R.id.tv_comment_name)
            TextView mTvCommentName;
            @Bind(R.id.ratingbarId_comments)
            XLHRatingBar mRatingbarIdComments;
            @Bind(R.id.tv_comments_data)
            TextView mTvCommentsData;
            @Bind(R.id.tv_comments_detail)
            TextView mTvCommentsDetail;

            public VH(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
